package com.lrabbit.abs;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Range;

import java.util.Date;

@Data
@ApiModel(description = "AbstractPageReq抽象公共分页请求对象(对应API模块的VO)")
public abstract class AbstractPageReq {

    /**
     * 分页开始页数，从1开始
     */
    @ApiModelProperty(value = "当前页码")
    @Range(min = 1, max = 999, message = "当前页码必须在0-999之间")
    private Integer pageIndex = 1;

    /**
     * 分页每页大小
     */
    @ApiModelProperty(value = "每页条数")
    @Range(min = 1, max = 999, message = "每页条数必须在1-999之间")
    private Integer pageSize = 15;

    /**
     * 排序字段
     */
    @ApiModelProperty(value = "排序字段", example = "create_time")
    private String lpSortName;

    /**
     * 排序方式
     */
    @ApiModelProperty(value = "升序: asc、desc", example = "desc")
    private String lpSortType;

    /**
     * 开始时间(结合lpEndDate使用)
     */
    @ApiModelProperty(value = "开始时间", example = "2024-06-08")
    private Date lpStartDate;

    /**
     * 结束时间(结合lpStartDate使用)
     */
    @ApiModelProperty(value = "结束时间(结合lpStartDate使用)", example = "2024-06-08")
    private Date lpEndDate;

    /**
     * 初始化分页对象【mybatis-plus】
     *
     * @return Page 分页对象
     */
    public Page initPage() {
        return new Page(pageIndex, pageSize);
    }

    /**
     * 排序字段 转换数据库映射字段
     * 规则：驼峰式命名转换为下划线命名
     *
     * @param lpSortName 排序字段
     */
    public void setLpSortName(String lpSortName) {
        if (lpSortName != null && !lpSortName.isEmpty()) {
            StringBuilder stringBuilder = new StringBuilder();
            char[] fieldChars = lpSortName.toCharArray();
            for (char fieldChar : fieldChars) {
                if (Character.isUpperCase(fieldChar)) {
                    stringBuilder.append("_").append(Character.toLowerCase(fieldChar));
                } else {
                    stringBuilder.append(fieldChar);
                }
            }
            this.lpSortName = stringBuilder.toString();
        } else {
            this.lpSortName = lpSortName;
        }
    }

}
